﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Kaskelot.SP.Services;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;

namespace Kaskelot.SP.UI.WebParts.PersonSearch
{
	[ToolboxItemAttribute(false)]
	public partial class PersonSearch : WebPart
	{
		// Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
		// using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
		// for production. Because the SecurityPermission attribute bypasses the security check for callers of
		// your constructor, it's not recommended for production purposes.
		// [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
		public PersonSearch()
		{
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			InitializeControl();
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (Page.IsPostBack) return;

			using (LookupService svc = new LookupService())
			{
				PersonSearchLookupResult pslr = svc.GetPersonSearchLookupData();

				IList<ListItem> typer = new List<ListItem>();
				foreach (PersonTyp v in pslr.PersonTyper)
				{
					typer.Add(new ListItem() { Text = v.Namn, Value = v.Id.ToString() });
				}
				TypRepeater.DataSource = typer;
				TypRepeater.DataBind();

				IList<ListItem> funktioner = new List<ListItem>();
				foreach (Funktion f in pslr.Funktioner)
				{
					funktioner.Add(new ListItem() { Text = f.Beskrivning, Value = f.Id.ToString() });
				}
				UppdragRepeater.DataSource = funktioner;
				UppdragRepeater.DataBind();
			}
		}
	}
}
